package com.hxgis.oc.cma.util;

import cn.hutool.json.JSONObject;
import com.hxgis.oc.config.OConfig;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public abstract class CmaFileRequest {
    public abstract String getDataTable();
    public abstract LocalDateTime parseTimeFromName(String fileName);
    public abstract List<CmaFileInfo> query(LocalDateTime fromTime, LocalDateTime toTime);

    public Long generateTimeStamp(String fileName)
    {
        return OConfig.genTimeStampFid(parseTimeFromName(fileName));
    }

    protected List<CmaFileInfo> toFileInfoList(List<JSONObject> list, Function<String, Long> funcGenID) {
        List<CmaFileInfo> res = new ArrayList<>();
        list.forEach(json -> {
            CmaFileInfo info = new CmaFileInfo();
            String fileName = json.getStr("FILE_NAME");
            info.setTimeStamp(funcGenID.apply(fileName));
            info.setFileName(fileName);
            info.setFormat(json.getStr("FORMAT"));
            info.setFileSize(json.getLong("FILE_SIZE"));
            String fileUrl = json.getStr("FILE_URL");
            String queryUri = fileUrl.split("\\?")[1];
            String[] filedValue = queryUri.split("&");
            for (String s : filedValue) {
                if (s.startsWith("url=")) {
                    info.setFurtive(s.substring(4));
                    break;
                }
            }
            res.add(info);
        });

        return res;
    }
}
